//
//  PhotoBrowserAnimator.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/20.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit

 /// MARK: - 展现转场协议
protocol PhotoBrowserPresentDelegate: NSObjectProtocol {

    //参与转场动画的图像视图
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    
    //展现转场图像起始位置
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect
    
    //展现转场图像结束位置
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect

}


 /// MARK: - 移除转场协议
protocol PhotoBrowserDismissDelegate: NSObjectProtocol {

    //解除转场动画视图
    func imageViewForDismiss() -> UIImageView
    
    // 解除转场动画索引
    func indexPathForDismiss() -> NSIndexPath

}

class PhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {

    //定义代理
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    
    //定义代理
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    
    //定义展现图片索引
    var indexPath: NSIndexPath?
    
    //设置代理参数
    func setDelegateParams(presentDelegate: PhotoBrowserPresentDelegate,indexPath: NSIndexPath,dismissDelegate: PhotoBrowserDismissDelegate) {
    
        self.presentDelegate = presentDelegate
        
        self.indexPath = indexPath
        
        self.dismissDelegate = dismissDelegate
    
    }
    
    
    //标记modal视图
    private var isPresent = false
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        
        //返回modal 展现的动画对象
        return self
        
    }
    
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = false
        
        return self
        
    }
    
}


///AMRK: - 实现具体的动画方法
extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {

    //动画的时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    //实现具体的动画效果
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //根据展示标记判断是展示视图还是移除视图
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
        
    }
    
    //解除动画
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
    
        guard let dismissDelegate = dismissDelegate, presentDelegate = presentDelegate else {
        
            return
        }
        
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        let iv = dismissDelegate.imageViewForDismiss()
        
        let indexPath = dismissDelegate.indexPathForDismiss()
        
        //添加到容器视图中
        transitionContext.containerView()?.addSubview(fromView!)
        transitionContext.containerView()?.addSubview(iv)
        fromView?.alpha = 0

        //设置动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            
            iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
            
            }) { (_) -> Void in
                
                //移除动画
                fromView?.removeFromSuperview()
                iv.removeFromSuperview()
                transitionContext.completeTransition(true)
                
        }
    
    
    }
    
    
    //展示动画
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
    
        guard let presentDelegate = presentDelegate, indexPath = indexPath else {
        
            return
        }
        
        
       let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        
        //添加到容器视图中
        transitionContext.containerView()?.addSubview(toView!)
        
        //设置动画
        toView?.alpha = 0
        
        //获取模型控制器
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! PhotoBrowserViewController
        
        //隐藏控制器
        toVC.collectionView.hidden = true
        
        //设置转场动画图像
        let iv = presentDelegate.imageViewForPresent(indexPath)
        
        //设置起始位置
        iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        
        //将图像添加到父容器中
        transitionContext.containerView()?.addSubview(iv)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            iv.frame = presentDelegate.photoBrowserPresentToRect(indexPath)
//            iv.alpha = 0
            
            toView?.alpha = 1.0
            
            
            }) { (_) -> Void in
                
                //删除图像视图
                iv.removeFromSuperview()
                toVC.collectionView.hidden = false
                
                transitionContext.completeTransition(true)
                
        }
    
    }
    

}